<?php

namespace common\models\business;

use Yii;

/**
 * This is the model class for table "business_saleorder".
 *
 * @property string $id
 * @property string $ordersn
 * @property integer $senddate
 * @property string $price
 * @property integer $status
 */
class BusinessSaleorder extends \yii\db\ActiveRecord
{


    //主要用于日志记录
    public $uselog = [
        'tabcomment' => '订购单',      //用于修改了什么的显示
        'mainfield' => 'id',      //主要的显示标题的字段
        'picturefield' => [],        //图片字段
        'timefield' => [],       //时间字段
        'unsetfiled' =>[],      //不需要记录的字段
    ];




    //状态
    const STATUS_NOMAL = 0;
    const STATUS_PAY = 1;
    const STATUS_SEND = 2;
    const STATUS_FINISH = 3;
    public static $statues = [
        self::STATUS_NOMAL => '正常',
        self::STATUS_PAY => '已付款',
        self::STATUS_SEND => '已送货',
        self::STATUS_FINISH => '已完成',
    ];



    //是否审核
    const IS_AGREE_DEFAULT = 0;
    const IS_AGREE_AGREE = 1;
    public static $agree = [
        self::IS_AGREE_DEFAULT => '未审核',
        self::IS_AGREE_AGREE => '已审核'
    ];



    const TYPE_BULK = 0;
    const TYPE_WHOLE = 1;
    public static $types = [
        self::TYPE_BULK => '库存单',
        self::TYPE_WHOLE => '整猪单',
    ];

    const NOPRE = 0;
    const ISPRE = 1;
    public static $ispre = [
        self::NOPRE => '销售出栏的生猪',
        self::ISPRE => '预售',
    ];

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'business_saleorder';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['ordersn','customerid'], 'required'],
            [['customerid','status','createtime','senddate','paytime','type','agreetime','ispre_sale'], 'integer'],
            [['price'], 'number'],
            [['ordersn','remark','customername'], 'string', 'max' => 255],
            [['inputuser','agreeuser'],'string','max'=>20],
            [['senddate','paytime'], 'default','value' => 0],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'ordersn' => '订货单号',
            'customerid' => '客户',
            'senddate' => '发送日期',
            'price' => '金额',
            'status' => '状态',
            'createtime' => '创建时间',
            'paytime' => '付款时间',
            'senddate' => '送货时间',
            'remark' => '备注',
            'type' => '单据类型',
            'inputuser'=>'数据录入人',
            'isagree' => '是否审核',
            'agreeuser' => '审核人',
            'agreetime' => '审核时间',
            'ispre_sale' => '是否整猪预售',
        ];
    }


    public function beforeSave($insert)
    {
        if($this->isNewRecord){
            $this->createtime = time();
            $this->inputuser = Yii::$app->user->identity->username;
        }

        if($this->status == self::STATUS_SEND){
            $this->senddate = time();
        }else if($this->status == self::STATUS_PAY){
            $this->paytime = time();
        }

        //大客户名称
        $cus = BusinessCustomer::findOne($this->customerid);
        $this->customername = $cus?$cus->name:'';

        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }


    /**
     * @return \yii\db\ActiveQuery
     * 订单的客户
     */
    public function getCustomer()
    {
        return $this->hasOne(BusinessCustomer::className(),['id'=>'customerid']);
    }


    /**
     * @return \yii\db\ActiveQuery
     * 获取库存订货单明细
     */
    public function getDetail()
    {
        return $this->hasMany(BusinessSaleorderDetail::className(),['saleorderid'=>'id']);
    }


    /**
     * @return \yii\db\ActiveQuery
     * 获取整猪订货单明细
     */
    public function getPigSaleDetail()
    {
        return $this->hasMany(BusinessSaleorderPigdetail::className(),['saleorderid' => 'id']);
    }



}
